﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.text.TextField;	
	import flash.events.Event;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.materials.shadematerials.CellMaterial;
	import org.papervision3d.materials.shadematerials.GouraudMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;

    
	/*
	 * coded by moka 2009 / 06/ 24 
	 * name : moka's Stroke3D
	 */
	
	public class Main extends Sprite 
	{		
		// 基本 PV3D 操作請參考 PV3D 基本教學
		private var scene:Scene3D = new Scene3D;
		private	var camera:Camera3D = new Camera3D;
		private	var viewport:Viewport3D = new Viewport3D(640, 480);
		private	var render:BasicRenderEngine = new BasicRenderEngine();
		private var bmp:Bitmap = addChild(new Bitmap(new BitmapData(640, 480, true, 0)))as Bitmap;
		private var matBuffer:BitmapData = new BitmapData(500, 500, true, 0);
		private var degree:Number;
		private var stroke3D:Plane;
		private var dummy:DisplayObject3D = new DisplayObject3D;
		
		public function Main():void 
		{			
			scene.addChild(new Sphere(new CellMaterial(new PointLight3D, 0xFFFFFF,0x00CCFF,10),25,16,12));
			matBuffer.draw(new Bitmap(new BitmapData(500,500,false,0xFFFFFF)));
			var mat:BitmapMaterial = new BitmapMaterial(matBuffer);
			mat.oneSide = false;
			stroke3D = scene.addChild(new Plane(mat, 500, 25, 60, 1)) as Plane;
			stroke3D.scale = 5;
			degree = 0;
			stage.scaleMode = "noScale"; 
			stage.addEventListener(Event.ENTER_FRAME, onProcess);
			(addChild(new TextField()) as TextField).htmlText = "<font color='#FFFFFF'>Moka's work 2009</font>"; 
		}		
		
		private function onProcess(e:Event):void {	
			var l:uint = stroke3D.geometry.vertices.length;
			var vs:Array = stroke3D.geometry.vertices;
			var delta:uint = 3;
			degree++;
			for (var i:uint = 0; i < l; i++ )
			{
				if (i == 0){
					vs[0].x += (mouseX-320 - vs[0].x) / delta;
					vs[0].y += ( -(mouseY - 240) - vs[0].y) / delta;
					vs[0].z = 100 * Math.cos(degree / 180 * Math.PI);
					
					dummy.x = vs[0].x;
					dummy.y = vs[0].y;
					dummy.z = vs[0].z;					
				}else {
					vs[i].x += (vs[i - 1].x - vs[i].x) / delta;					
					vs[i].y += (vs[i - 1].y - vs[i].y) / delta;
					vs[i].z += (vs[i - 1].z - vs[i].z) / delta;
				}	
			}			
			
			camera.x +=  (vs[l-1].x - camera.x) / (delta*4);
			camera.y +=  (vs[l-1].y - camera.y) / (delta*4);
			camera.z +=  (vs[l-1].z - camera.z) / (delta*4);
			
			var bmpBuffer:BitmapData = new BitmapData(640, 480, true, 0);
			bmp.bitmapData.fillRect(bmp.bitmapData.rect, 0xCC000000);		
			camera.lookAt(dummy);
			render.renderScene(scene, camera, viewport);
			bmpBuffer.draw(viewport);
			bmpBuffer.applyFilter(bmpBuffer, bmpBuffer.rect, new Point, new GlowFilter(0x0066FF, 1, 20, 20, 5, 3));			
			bmp.bitmapData.draw(bmpBuffer);
		}   
	}	
}